package cn.stylefeng.guns.modular.business.frequency.cmcompany.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.exception.enums.StatusExceptionEnum;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.AndaPageResult;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.modular.business.custom.enums.YesOrNoEnum;
import cn.stylefeng.guns.modular.business.frequency.cmcity.entity.CmCity;
import cn.stylefeng.guns.modular.business.frequency.cmcity.mapper.CmCityMapper;
import cn.stylefeng.guns.modular.business.frequency.cmcompany.entity.CmCompany;
import cn.stylefeng.guns.modular.business.frequency.cmcompany.enums.CmCompanyExceptionEnum;
import cn.stylefeng.guns.modular.business.frequency.cmcompany.mapper.CmCompanyMapper;
import cn.stylefeng.guns.modular.business.frequency.cmcompany.model.export.CompanyLinkmanExport;
import cn.stylefeng.guns.modular.business.frequency.cmcompany.model.param.*;
import cn.stylefeng.guns.modular.business.frequency.cmcompany.model.result.*;
import cn.stylefeng.guns.modular.business.frequency.cmcompany.service.CmCompanyService;
import cn.stylefeng.guns.modular.business.frequency.cmcompanyline.entity.CmCompanyLine;
import cn.stylefeng.guns.modular.business.frequency.cmcompanyline.mapper.CmCompanyLineMapper;
import cn.stylefeng.guns.modular.business.frequency.cmcompanyorder.mapper.CmCompanyOrderMapper;
import cn.stylefeng.guns.modular.business.frequency.cmdriver.entity.CmDriver;
import cn.stylefeng.guns.modular.business.frequency.cmdriver.mapper.CmDriverMapper;
import cn.stylefeng.guns.modular.business.frequency.cmlinkman.entity.CmLinkman;
import cn.stylefeng.guns.modular.business.frequency.cmlinkman.mapper.CmLinkmanMapper;
import cn.stylefeng.guns.modular.business.frequency.orderpick.mapper.OrderPickMapper;
import cn.stylefeng.guns.modular.business.frequency.ordertake.mapper.OrderTakeMapper;
import cn.stylefeng.guns.modular.business.frequency.pricelibrary.listener.excel.CompanyLinkmanListener;
import cn.stylefeng.guns.modular.business.frequency.transport.mapper.TransportMapper;
import cn.stylefeng.guns.modular.business.util.BusinessUtil;
import cn.stylefeng.guns.sys.modular.util.SnowIdUtils;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 通讯录表 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:17
 */
@Slf4j
@Service
public class CmCompanyServiceImpl extends ServiceImpl<CmCompanyMapper, CmCompany> implements CmCompanyService {

    @Resource
    private CmLinkmanMapper linkmanMapper;
    @Resource
    private CmDriverMapper driverMapper;
    @Autowired
    private BusinessUtil businessUtil;
    @Resource
    private CmCompanyOrderMapper companyOrderMapper;
    @Resource
    private OrderPickMapper pickCarMapper;
    @Resource
    private CmCityMapper cmCityMapper;
    @Resource
    private OrderTakeMapper orderTakeMapper;
    @Resource
    private TransportMapper transportMapper;
    @Resource
    private CmCompanyLineMapper companyLineMapper;
    @Resource
    private CmCompanyMapper companyMapper;


    @Override
    public PageResult<CmCompany> page(CmCompanyParam cmCompanyParam) {

        // 构造条件
        LambdaQueryWrapper<CmCompany> queryWrapper = new LambdaQueryWrapper<>();


        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<CmCompany> list(CmCompanyParam cmCompanyParam) {

        // 构造条件
        LambdaQueryWrapper<CmCompany> queryWrapper = new LambdaQueryWrapper<>();


        return this.list(queryWrapper);
    }

    @Override
    public void add(CmCompanyParam cmCompanyParam) {

        // 将dto转为实体
        CmCompany cmCompany = new CmCompany();
        BeanUtil.copyProperties(cmCompanyParam, cmCompany);


        this.save(cmCompany);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(CmCompanyParam cmCompanyParam) {

        // 根据id查询实体
        CmCompany cmCompany = this.queryCmCompany(cmCompanyParam);


        // 更新实体
        this.updateById(cmCompany);

    }

    @Override
    public void edit(CmCompanyParam cmCompanyParam) {

        // 根据id查询实体
        CmCompany cmCompany = this.queryCmCompany(cmCompanyParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(cmCompanyParam, cmCompany);

        // 不能修改状态，用修改状态接口修改状态
        cmCompany.setStatus(null);

        this.updateById(cmCompany);
    }

    @Override
    public CmCompany detail(CmCompanyParam cmCompanyParam) {
        return this.queryCmCompany(cmCompanyParam);
    }

    @Override
    public void changeStatus(CmCompanyParam cmCompanyParam) {
        Long id = cmCompanyParam.getId();
        Integer status = cmCompanyParam.getStatus();


        // 更新枚举，更新只能更新未删除状态的
        LambdaUpdateWrapper<CmCompany> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(CmCompany::getId, id)
                .set(CmCompany::getStatus, status);
        boolean update = this.update(updateWrapper);
        if (!update) {
            throw new ServiceException(StatusExceptionEnum.UPDATE_STATUS_ERROR);
        }
    }

    @Override
    public ResponseData getCompanyListPage(CompanyDTO companyDTO, List<Long> userId) {
        //通过创建人查询联系人id
        List<Long> companyId = this.baseMapper.getCompanyIdList(userId);
        companyId.add(-104l);
        Page<PkgCompany> result = this.baseMapper.getCompanyListPage(PageFactory.defaultPage(), companyDTO, companyId,businessUtil.getTenantId());
        // 合计行
        PkgCompany pkgCompany = this.baseMapper.getTableSummary(companyDTO, companyId,businessUtil.getTenantId());
        AndaPageResult<Object> pageResult = new AndaPageResult<>();
        pageResult.AndaPageResult(result, companyDTO);
        pageResult.setSummary(pkgCompany);
        return ResponseData.andaSuccess(pageResult);
    }

    @Override
    public ResponseData getLinkmanListPage(String companyId, CompanyDTO companyDTO) {
        Page<PkgLinkman> page = linkmanMapper.getLinkmanListPage(PageFactory.defaultPage(), companyId, companyDTO, businessUtil.getTenantId());
        return ResponseData.andaSuccess(new AndaPageResult<>().AndaPageResult(page, companyDTO));
    }

    @Override
    public boolean checkCompanyAdd(CompanyVO companyVO, StringBuilder errorMsg) {
        // 公司名称不可重复
        if (StringUtils.isNotBlank(companyVO.getName()) && this.baseMapper.countByName(companyVO.getName(), null, businessUtil.getTenantId()) > 0) {
            errorMsg.append("已存在相同公司名称，请检查");
            return false;
        }
        // 公司名称不可重复
        if (StringUtils.isNotBlank(companyVO.getName()) && this.baseMapper.countByPhone(companyVO.getName(), null, businessUtil.getTenantId()) > 0) {
            errorMsg.append("已存在相同联系人手机号，请检查");
            return false;
        }
        // 联系人电话不可重复
        if (companyVO.getLinkmanList() != null || companyVO.getLinkmanList().size() > 0) {
            for (CmLinkman linkman : companyVO.getLinkmanList()) {
                if (!Validator.isMobile(linkman.getPhone())) {
                    errorMsg.append("【" + linkman.getPhone() + "】号码格式错误！");
                    return false;
                }
                if (linkmanMapper.countByPhone(linkman.getPhone(), null, businessUtil.getTenantId()) > 0) {
                    errorMsg.append("【" + linkman.getPhone() + "】号码已存在！");
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void addCompany(CompanyVO companyVO) {
        Long tenantId = businessUtil.getTenantId();

        CmCompany company = new CmCompany();
        company.setType(companyVO.getType());
        company.setNature(companyVO.getNature());
        company.setContract(companyVO.getContract());
        company.setName(companyVO.getName());
        company.setTenantId(tenantId);
        this.baseMapper.insert(company);
        // 新增联系人
        if (companyVO.getLinkmanList() != null && companyVO.getLinkmanList().size() > 0) {
            for (CmLinkman linkman : companyVO.getLinkmanList()) {
                linkman.setTenantId(tenantId);
                linkman.setCompanyId(company.getId());
                linkmanMapper.insert(linkman);
            }
        }
        // 新增路线
        if (companyVO.getLineList() != null && companyVO.getLineList().size() > 0) {
            for (CmCompanyLine line : companyVO.getLineList()) {
                if (line != null && StringUtils.isNotBlank(line.getStartCode()) && StringUtils.isNotBlank(line.getDestinationCode())) {
                    line.setCompanyId(company.getId());
                    line.setTenantId(tenantId);
                    line.setLineName(getLineNameByCityCode(line.getStartCode(), line.getDestinationCode()));
                    companyLineMapper.insert(line);
                }
            }
        }
    }

    @Override
    public boolean checkCompanyUpdate(CompanyVO companyVO, StringBuilder errorMsg) {
        CmCompany oc = this.getById(companyVO.getId());
        // 公司名称不可重复
        if (companyVO.getName()!=null&&!companyVO.getName().equals(oc.getName()) && this.baseMapper.countByName(companyVO.getName(), companyVO.getId(), businessUtil.getTenantId()) > 0) {
            errorMsg.append("已存在相同公司名称，请检查");
            return false;
        }
        return true;
    }

    @Override
    public void updateCompany(CompanyVO companyVO) {
        Long tenantId = businessUtil.getTenantId();
        CmCompany company = new CmCompany();
        company.setTenantId(tenantId);
        BeanUtils.copyProperties(companyVO, company);
        this.baseMapper.updateById(company);
        if (companyVO.getLinkmanList() != null && companyVO.getLinkmanList().size() > 0) {
            for (CmLinkman linkman : companyVO.getLinkmanList()) {
                if (linkman.getId() != null) {
                    linkman.setTenantId(tenantId);
                    linkmanMapper.updateById(linkman);
                } else {
                    linkman.setTenantId(tenantId);
                    linkman.setCompanyId(Long.valueOf(companyVO.getId()));
                    linkmanMapper.insert(linkman);
                }
            }
        }
        if (companyVO.getLineList() != null && companyVO.getLineList().size() > 0) {
            for (CmCompanyLine line : companyVO.getLineList()) {
                if (line != null && StringUtils.isNotBlank(line.getStartCode()) && StringUtils.isNotBlank(line.getDestinationCode())) {
                    if (line.getId() != null) {
                        line.setTenantId(tenantId);
                        line.setLineName(getLineNameByCityCode(line.getStartCode(), line.getDestinationCode()));
                        companyLineMapper.updateById(line);
                    } else {
                        line.setTenantId(tenantId);
                        line.setCompanyId(Long.valueOf(companyVO.getId()));
                        line.setLineName(getLineNameByCityCode(line.getStartCode(), line.getDestinationCode()));
                        companyLineMapper.insert(line);
                    }
                }
            }
        }
    }

    @Override
    public CompanyVO getCompanySingleDetail(String id) {
        CompanyVO companyVO = this.baseMapper.getCompanyVOById(id);
        companyVO.setLinkmanList(linkmanMapper.selectLinkmanListByCompanyId(companyVO.getId()));
        companyVO.setLineList(companyLineMapper.selectListByCompanyId( companyVO.getId()));
        return companyVO;
    }

    @Override
    public CmCompany getCompanyById(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public void deleteCompany(String id) {
        this.baseMapper.deleteCompany(id);
        linkmanMapper.deleteByCompanyId(id);
        driverMapper.deleteByCompanyId(id);
    }

    @Override
    public boolean checkLinkmanAdd(CompanyVO companyVO, StringBuilder errorMsg) {
        if (companyVO == null || companyVO.getLinkmanList() == null || companyVO.getLinkmanList().size() == 0) {
            errorMsg.append("请至少添加一个联系人");
            return false;
        }
        // 联系人电话不可重复
        if (companyVO.getLinkmanList() != null || companyVO.getLinkmanList().size() > 0) {
            for (CmLinkman linkman : companyVO.getLinkmanList()) {
                if (!Validator.isMobile(linkman.getPhone())) {
                    errorMsg.append("【" + linkman.getPhone() + "】号码格式错误！");
                    return false;
                }
                if (linkmanMapper.countByPhone(linkman.getPhone(), null, businessUtil.getTenantId()) > 0) {
                    errorMsg.append("【" + linkman.getPhone() + "】号码已存在！");
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void addLinkman(CompanyVO companyVO) {
        for (CmLinkman linkman : companyVO.getLinkmanList()) {
            linkman.setCompanyId(Long.valueOf(companyVO.getId()));
            linkmanMapper.insert(linkman);
        }
    }

    @Override
    public CmLinkman getLinkmanSingleDetail(String id) {
        return linkmanMapper.selectById(id);
    }

    @Override
    public void deleteLinkman(String id) {
        linkmanMapper.deleteLinkman(id);
    }

    @Override
    public boolean checkLinkmanUpdate(CmLinkman linkman, StringBuilder errorMsg) {
        if (linkman == null | linkman.getId() != null) {
            errorMsg.append("参数错误");
            return false;
        }
        if (!Validator.isMobile(linkman.getPhone())) {
            errorMsg.append("【" + linkman.getPhone() + "】号码格式错误！");
            return false;
        }
        if (linkmanMapper.countByPhone(linkman.getPhone(), linkman.getId(), businessUtil.getTenantId()) > 0) {
            errorMsg.append("【" + linkman.getPhone() + "】号码已存在！");
            return false;
        }
        return true;
    }

    private String getLineNameByCityCode(String startCode, String destinationCode) {
        CmCity startCity = cmCityMapper.getCityByCode(startCode);
        CmCity destinationCity = cmCityMapper.getCityByCode(destinationCode);
        if (startCity != null && destinationCity != null) {
            return startCity.getName() + "---" + destinationCity.getName();
        } else {
            return Strings.EMPTY;
        }
    }

    public void updateLinkman(CmLinkman linkman) {
        linkmanMapper.updateById(linkman);
    }

    @Override
    public List<CmDriver> getDriverListPage(String companyId, CompanyDTO companyDTO) {
        return driverMapper.getDriverListPage(companyId, companyDTO,businessUtil.getTenantId());
    }

    @Override
    public boolean checkDriverAdd(CmDriver driver, StringBuilder errorMsg) {
        errorMsg.append(driver.getName());
        if (driver == null || driver.getCompanyId() == null) {
            errorMsg.append("参数错误");
            return false;
        }
        if (!Validator.isMobile(driver.getPhone())) {
            errorMsg.append("【" + driver.getPhone() + "】号码格式错误");
            return false;
        }
        if (driver == null || StringUtils.isBlank(driver.getCardNumber()) ) {
            errorMsg.append("身份证号不能为空");
            return false;
        }
        // 号码不可重复
        if (driverMapper.countByPhone(driver.getPhone(), null, businessUtil.getTenantId()) > 0) {
            errorMsg.append("【" + driver.getPhone() + "】号码已存在");
            return false;
        }
        errorMsg = new StringBuilder();
        driver.setId(SnowIdUtils.uniqueLong());
        driver.setCreateUser(LoginContextHolder.me().getSysLoginUserId());
        driver.setTenantId(businessUtil.getTenantId());
        return true;
    }

    @Override
    public void addDriver(CmDriver driver) {
        Long tenantId = businessUtil.getTenantId();
        driver.setTenantId(tenantId);
        driverMapper.insert(driver);
    }

    @Override
    public boolean checkDriverUpdate(CmDriver driver, StringBuilder errorMsg) {
        if (driver == null || driver.getId() == null) {
            errorMsg.append("参数错误");
            return false;
        }
        if (!Validator.isMobile(driver.getPhone())) {
            errorMsg.append("【" + driver.getPhone() + "】号码格式错误");
            return false;
        }
        if (driver == null || StringUtils.isBlank(driver.getCardNumber()) ) {
            errorMsg.append("参数错误");
            return false;
        }
        // 号码不可重复
        if (driverMapper.countByPhone(driver.getPhone(), driver.getId(), businessUtil.getTenantId()) > 0) {
            errorMsg.append("【" + driver.getPhone() + "】号码已存在");
            return false;
        }
        return true;
    }

    @Override
    public void updateDriver(CmDriver driver) {
        driverMapper.updateById(driver);
    }

    @Override
    public CmDriver getDriverDetail(String id) {
        return driverMapper.selectById(id);
    }

    @Override
    public void deleteDriver(String id) {
        driverMapper.deleteDriver(id);
    }

    @Override
    public Page<PkgCompanyOrder> getCompanyOrderListPage(String companyId, Page<Object> defaultPage, CompanyOrderDTO companyOrderDTO) {
        return companyOrderMapper.getCompanyOrderListPage(companyId, defaultPage, companyOrderDTO);
    }

    @Override
    public boolean checkUnCheckLinkmanAdd(UnCheckCompanyVO companyVO, StringBuilder errorMsg) {
        // 客户联系人电话不可重复
        if (linkmanMapper.countByPhone(companyVO.getLinkmanPhone(), null, businessUtil.getTenantId()) > 0) {
            errorMsg.append("联系人电话已存在");
            return false;
        }
        return true;
    }

    @Override
    public UnCheckCompanyVO addUnCheckLinkman(UnCheckCompanyVO companyVO) {


        CmCompany company = new CmCompany();
        company.setHasCheck(YesOrNoEnum.NO.type);// 待审核
        company.setName(companyVO.getCompanyName());
        company.setTenantId(businessUtil.getTenantId());
        company.setType(companyVO.getType());
        company.setNature(companyVO.getNature());
        company.setHasSelf(companyVO.getHasSelf());
        if(companyVO.getHasSelf()==1){
            CmCompany oldCompany = new CmCompany();
            oldCompany.setHasSelf(0);
            this.update(oldCompany,new QueryWrapper<CmCompany>().lambda().eq(CmCompany::getTenantId,businessUtil.getTenantId()));
        }

        this.baseMapper.insert(company);
        // 联系人
        CmLinkman linkman = new CmLinkman();
        linkman.setName(companyVO.getLinkmanName());
        linkman.setPhone(companyVO.getLinkmanPhone());
        linkman.setCompanyId(company.getId());
        linkman.setTenantId(businessUtil.getTenantId());
        //linkman.setLastOrderTime(DateUtil.formatDateTime(new Date()));
        linkman.setHasCheck(YesOrNoEnum.NO.type);
        linkmanMapper.insert(linkman);
        companyVO.setCompanyId(company.getId().toString());
        companyVO.setLinkmanId(linkman.getId().toString());
        return companyVO;
    }

    @Override
    public LinkmanCheckVO getLinkmanCheckDetail(String id) {
        return linkmanMapper.getLinkmanCheckDetail(id);
    }

    @Override
    public boolean checkLinkmanCheck(LinkmanCheckVO linkmanCheckVO, StringBuilder errorMsg) {
        // 联系人电话不可重复
        if (linkmanMapper.countByPhone(linkmanCheckVO.getPhone(), Long.valueOf(linkmanCheckVO.getId()), businessUtil.getTenantId()) > 0) {
            errorMsg.append("联系人号码已存在");
            return false;
        }
        return true;
    }

    @Override
    public void checkLinkman(LinkmanCheckVO linkmanCheckVO) {
        CmLinkman originLinkman = linkmanMapper.selectById(linkmanCheckVO.getId());
        //Company originCompany = companyMapper.selectById(originLinkman.getCompanyId());
        CmLinkman linkman = new CmLinkman();
        linkman.setId(originLinkman.getId());
        linkman.setName(linkmanCheckVO.getName());
        linkman.setPhone(linkmanCheckVO.getPhone());
        linkman.setCity(linkmanCheckVO.getCity());
        linkman.setCompanyId(Long.valueOf(linkmanCheckVO.getCompanyId()));
        linkman.setHasCheck(YesOrNoEnum.YES.type);
        linkman.setCheckUser(LoginContextHolder.me().getSysLoginUserId().toString());
        linkmanMapper.updateById(linkman);
        this.baseMapper.updateHasCheck(originLinkman.getCompanyId().toString());
        // 如果审核时换了公司，则将提交的公司软删除
        if (!linkmanCheckVO.getCompanyId().equals(originLinkman.getCompanyId())) {
            this.baseMapper.deleteCompany(originLinkman.getCompanyId().toString());
            // 其他业务表中公司ID变更
            companyOrderMapper.updateCompanyIdByLinkmanId(linkmanCheckVO.getCompanyId(), linkmanCheckVO.getId());
            pickCarMapper.updateCompanyIdByLinkmanId(linkmanCheckVO.getCompanyId(), linkmanCheckVO.getId());
            orderTakeMapper.updateCompanyIdByLinkmanId(linkmanCheckVO.getCompanyId(), linkmanCheckVO.getId());
            transportMapper.updateCompanyIdByLinkmanId(linkmanCheckVO.getCompanyId(), linkmanCheckVO.getId());
        } else {
            // 没有换公司，则更新公司信息
            CmCompany company = new CmCompany();
            company.setId(originLinkman.getCompanyId());
            company.setName(linkmanCheckVO.getCompanyName());
            company.setType(linkmanCheckVO.getCompanyType());
            company.setNature(linkmanCheckVO.getCompanyNature());
            this.baseMapper.updateById(company);
        }
    }

    @Override
    public List<UnCheckCompanyVO> getHasCheckCompanyList() {
        return this.baseMapper.getHasCheckCompanyList(businessUtil.getTenantId());
    }

    @Override
    public List<OrderLinkmanVO> getOrderLinkmanSuggest(OrderLinkmanVO orderLinkmanVO) {
        Long tenantId = businessUtil.getTenantId();
        return linkmanMapper.getOrderLinkmanSuggest(orderLinkmanVO, tenantId);
    }

    @Override
    public List<OrderLinkmanVO> getOrderLinkmanSuggestAll(OrderLinkmanVO orderLinkmanVO) {
        Long tenantId = businessUtil.getTenantId();
        return linkmanMapper.getOrderLinkmanSuggestAll(orderLinkmanVO, tenantId);
    }


    @Override
    public List<OrderLinkmanVO> getLinkmanListByIds(List<String> linkmanIds) {
        return linkmanMapper.getLinkmanListByIds(linkmanIds);
    }

    @Override
    public List<CmCompanyLine> getCompanyLineListById(String companyId) {
        return companyLineMapper.selectList(new QueryWrapper<CmCompanyLine>().lambda()
                .eq(CmCompanyLine::getCompanyId, companyId));
    }

    @Override
    public void importExcel(MultipartFile file) {
        long startTime = System.currentTimeMillis();
        ExcelReader excelReader = null;
        InputStream in = null;
        try {
            in = file.getInputStream();
            excelReader = EasyExcel.read(in, CompanyLinkmanExport.class, new CompanyLinkmanListener(companyMapper, linkmanMapper, businessUtil)).build();
            ReadSheet readSheet = EasyExcel.readSheet(0).build();
            excelReader.read(readSheet);
        } catch (Exception e) {
            log.error("import excel to db fail", e);
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (excelReader != null) {
                excelReader.finish();
            }
        }
        long endTime = System.currentTimeMillis();
        long totalTime = endTime - startTime;
        log.info("【消耗时间为】{}", totalTime);
    }

    @Override
    public void changeLinkmanOwnerUser(CmLinkman linkman) {
        linkmanMapper.changeLinkmanOwnerUser(linkman.getId(), linkman.getCreateUser());
    }

    @Override
    public void setHasSelf(Long id) {
        CmCompany company = new CmCompany();
        company.setHasSelf(0);
        this.update(company,new QueryWrapper<CmCompany>().lambda().eq(CmCompany::getTenantId,businessUtil.getTenantId()));
        company.setId(id);
        company.setHasSelf(1);
        this.updateById(company);
    }


    /**
     * 获取通讯录表
     *
     * @author yujie
     * @since 2023/05/09 17:17
     */
    private CmCompany queryCmCompany(CmCompanyParam cmCompanyParam) {
        CmCompany cmCompany = this.getById(cmCompanyParam.getId());
        if (ObjectUtil.isEmpty(cmCompany)) {
            throw new ServiceException(CmCompanyExceptionEnum.NOT_EXISTED);
        }
        return cmCompany;
    }
}